package dao;

import beans.ClassBean;
import beans.FAQbean;
import beans.FeedBackBean;
import helper.ReadConfig;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class FeedBackDAO {

    public boolean createFeedBack(FeedBackBean fbBean) {
        try {
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "insert into tbl_FeedBack(Acc_Id, Question, Class_Id)"
                    + " values(" + Integer.parseInt(fbBean.getAcc_id()) + ",'"
                    + fbBean.getQuestion() + "',"
                    + Integer.parseInt(fbBean.getClass_id()) + ")";
            System.out.println(sSQL);
            Statement stm = con.createStatement();
            int rs = stm.executeUpdate(sSQL);
            System.out.println(rs);
            if (rs == 1) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //
    public boolean updateFeedBack(FeedBackBean fbBean) {
        try {
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "Update tbl_FeedBack set "
                    + " Acc_Id=" + Integer.parseInt(fbBean.getAcc_id())
                    + ", Answer='" + fbBean.getAnswer()
                    + " ' WHERE FeedBack_Id = " + fbBean.getFeedback_id();
            System.out.println(sSQL);
            Statement stm = con.createStatement();
            int rs = stm.executeUpdate(sSQL);
            System.out.println(rs);
            if (rs == 1) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteFeedBack(String fb_id) {
        try {
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "DELETE FROM tbl_FeedBack WHERE FeedBack_Id = " + Integer.parseInt(fb_id);
            Statement stm = con.createStatement();
            int rs = stm.executeUpdate(sSQL);
            if (rs == 1) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //
    public List<FeedBackBean> showFeedBack() {
        try {
            List<FeedBackBean> listFB = new ArrayList<FeedBackBean>();
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "select * from tbl_FeedBack";
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sSQL);
            while (rs.next()) {
                FeedBackBean fbBean = new FeedBackBean();
                fbBean.setFeedback_id(rs.getString(1));
                fbBean.setAcc_id(rs.getString(2));
                fbBean.setQuestion(rs.getString(3));
                fbBean.setAnswer(rs.getString(4));
                fbBean.setClass_id(rs.getString(5));
                listFB.add(fbBean);
            }
            rs.close();
            stm.close();
            return listFB;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //
    public List<FeedBackBean> showFeedBackById(int acc_id) {
        try {
            List<FeedBackBean> listFB = new ArrayList<FeedBackBean>();
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "select * from tbl_FeedBack where Acc_Id=" + acc_id;
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sSQL);
            while (rs.next()) {
                FeedBackBean fbBean = new FeedBackBean();
                fbBean.setFeedback_id(rs.getString(1));
                fbBean.setAcc_id(rs.getString(2));
                fbBean.setQuestion(rs.getString(3));
                fbBean.setAnswer(rs.getString(4));
                fbBean.setClass_id(rs.getString(5));
                listFB.add(fbBean);
            }
            rs.close();
            stm.close();
            return listFB;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<FeedBackBean> showFeedBackByClass(List<ClassBean> listClass) {
        try {
            List<FeedBackBean> listFB = new ArrayList<FeedBackBean>();
            for (ClassBean classBean : listClass) {
                ReadConfig readConf = new ReadConfig();
                Connection con = readConf.getDBConnection();
                String sSQL = "select * from tbl_FeedBack where Class_Id=" + Integer.parseInt(classBean.getClass_id());
                Statement stm = con.createStatement();
                ResultSet rs = stm.executeQuery(sSQL);
                while (rs.next()) {
                    FeedBackBean fbBean = new FeedBackBean();
                    fbBean.setFeedback_id(rs.getString(1));
                    fbBean.setAcc_id(rs.getString(2));
                    fbBean.setQuestion(rs.getString(3));
                    fbBean.setAnswer(rs.getString(4));
                    fbBean.setClass_id(rs.getString(5));
                    listFB.add(fbBean);
                }
                rs.close();
                stm.close();
            }
            return listFB;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<FeedBackBean> showFeedBackByClassID(String class_id) {
        try {
            List<FeedBackBean> listFB = new ArrayList<FeedBackBean>();
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "select * from tbl_FeedBack where Class_Id=" + class_id;
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sSQL);
            while (rs.next()) {
                FeedBackBean fbBean = new FeedBackBean();
                fbBean.setFeedback_id(rs.getString(1));
                fbBean.setAcc_id(rs.getString(2));
                fbBean.setQuestion(rs.getString(3));
                fbBean.setAnswer(rs.getString(4));
                fbBean.setClass_id(rs.getString(5));
                listFB.add(fbBean);
            }
            rs.close();
            stm.close();
            return listFB;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
